package com.briup.web.servlet;

import com.briup.bean.Customer;
import com.briup.bean.ShopAddress;
import com.briup.bean.ShopCar;
import com.briup.service.IShopAddressService;
import com.briup.service.IShopCarService;
import com.briup.service.impl.ShopAddressServiceImpl;
import com.briup.service.impl.ShopCarServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "ShopCarCommitServlet", value = "/shopCarCommit")
public class ShopCarCommitServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        IShopAddressService shopAddressService = new ShopAddressServiceImpl();
        IShopCarService shopCarService = new ShopCarServiceImpl();
        //这个servlet用于提交订单
        HttpSession session = req.getSession();
        Customer customer = (Customer) session.getAttribute("customer");
        Integer customerId = customer.getId();
        //首先你要查询当前用户的所有收货地址存到session以便confirm显示
        List<ShopAddress> addressList = shopAddressService.findAddressByCustomerId(customerId);
        session.setAttribute("addressList",addressList);
        //清空当前用户的购物车
        shopCarService.deleteShopCarByCustomerId(customerId);
        //把session里的购物车记录 放到一个confirmList里
        List<ShopCar> shopCarList = (List<ShopCar>) session.getAttribute("shopCarList");
        session.setAttribute("confirm",shopCarList);
//        移除原本的shopCarList
        session.removeAttribute("shopCarList");
//        跳转到confirm.jsp
        resp.sendRedirect("/confirm.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}